package team.szm.scd.database.dto;

public class Result<T> {
    private Integer code;
    private String message;
    private T data;

    public static class Code {
        public final static Integer SUCCESS = 0;
        public final static Integer FAIL = 1;
        public final static Integer NEED_LOGIN = FAIL | 2;
        public final static Integer PERMISSION_DENIED = FAIL | 4;
    }

    public static class Message {
        public static final String SUCCESS = "success";
        public static final String FAIL = "fail";
        public static final String NEED_LOGIN = "请您先登录！";

        public static final String PERMISSION_DENIED = "您无权限操作！";
    }

    public static <T> Result<T> needLogin() {
        return new Result<>(Result.Code.NEED_LOGIN, Result.Message.NEED_LOGIN, null);
    }

    public static <T> Result<T> permissionDenied() {
        return new Result<>(Result.Code.PERMISSION_DENIED, Result.Message.PERMISSION_DENIED, null);
    }

    public static <T> Result<T> success(String message, T data) {
        return new Result<>(Code.SUCCESS, message, data);
    }

    public static <T> Result<T> success(T data) {
        return success(Message.SUCCESS, data);
    }

    public static Result<Object> success() {
        return success(null);
    }

    public static Result<Object> success(String message) {
        return success(message, null);
    }

    public static <T> Result<T> fail(String message, T data) {
        return new Result<>(Code.FAIL, message, data);
    }

    public static <T> Result<T> fail(T data) {
        return fail(Message.FAIL, data);
    }

    public static Result<Object> fail() {
        return fail(null);
    }

    public static Result<Object> fail(String message) {
        return fail(message, null);
    }

    public Result(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Result(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
